package com.leilei.gateway.controller;

import com.leilei.gateway.entity.Result;
import com.leilei.gateway.entity.form.RouteForm;
import com.leilei.gateway.service.DynamicRouteService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lei
 * @create 2022-04-11 17:06
 * @desc
 **/
@RestController
@RequestMapping("/api/route")
public class RouteRestController {

    private final DynamicRouteService dynamicRouteService;

    public RouteRestController(DynamicRouteService dynamicRouteService) {
        this.dynamicRouteService = dynamicRouteService;
    }

    /**
     * 增加路由
     *
     * @param routeForm 路由模型
     *
     * @return
     */
    @PostMapping
    public Result<Boolean> add(@RequestBody RouteForm routeForm) {
        return this.dynamicRouteService.add(routeForm);
    }


    /**
     * 更新路由
     *
     * @param routeForm 路由模型
     * @return
     */
    @PutMapping
    public Result update(@RequestBody RouteForm routeForm) {
        return this.dynamicRouteService.update(routeForm);
    }

    /**
     * 删除路由
     *
     * @param id 路由Id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id) {
        Result<Boolean> delete = this.dynamicRouteService.delete(id);
        return Result.success();
    }

    @GetMapping("/flush")
    public Result flush() {
        dynamicRouteService.flushRoute();
        return Result.success();
    }
}